package com.sk.control;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.sk.domain.Good;
import com.sk.service.GoodService;

@Controller
@RequestMapping("/good")
public class GoodControl extends BaseControl {
	@Autowired
	private GoodService gs;
	@RequestMapping("findAll")
	public String findAll(){
		try{
		session.setAttribute("goods", gs.findAll());
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "goods";
	}
	@RequestMapping("addGood")
	@POST
	public String addGood(@RequestParam("name")String name,@RequestParam("price")double price,@RequestParam("file")MultipartFile file){
		try{
			gs.addGood(name,price,file,request);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "springmvc/good/findAll";
	}
	@RequestMapping("delGood")
	@GET
	public String delGood(@RequestParam("id")int  id){
		try{
			gs.delByID(id);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "springmvc/good/findAll";
	}
	
	
	
	
	@RequestMapping("findByID")
	public String findByID(@RequestParam("id")Integer  id){
		try{
			if(gs == null){
				System.out.println("gs == null");
			}
			Good g =  gs.findByID(id);
			session.setAttribute("good",g);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "updatepro";
	}
	@RequestMapping("updateGood")
	@POST
	public String updateUpload(@RequestParam("id")int id,@RequestParam("name")String name,@RequestParam("price")double price,@RequestParam("file")MultipartFile file){
		try{
			gs.updateGood(id, file, name, price, request);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "springmvc/good/findAll";
	}

}
